package com.longge;

import java.util.Arrays;

public class LargestPerimeterII {

    public long largestPerimeter(int[] nums) {

        Arrays.sort(nums);

        long ans = -1, sum = nums[0];
        for (int i = 1; i < nums.length - 1; i++) {
            sum += nums[i];
            if (sum > nums[i + 1]) {
                ans = Math.max(ans, sum + nums[i + 1]);
            }
        }


        return ans;
    }
}
